package com.muyu.breakdown.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.muyu.breakdown.domain.SysCarFault;
import com.muyu.breakdown.service.SysCarFaultService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: YZL
 * @Created: 2024/9/20 11:00
 */
@RestController
@RequestMapping("/breakdown")
public class SysCarFaultController extends BaseController
{
    @Resource
    private SysCarFaultService sysCarFaultService;

    /**
     * 查询车辆故障管理列表
     */
    @RequiresPermissions("breakdown:breakdown:list")
    @GetMapping("/list")
    public Result<TableDataInfo<SysCarFault>> list(SysCarFault sysCarFault)
    {
        startPage();
        List<SysCarFault> list = sysCarFaultService.selectSysCarFaultList(sysCarFault);
        return getDataTable(list);
    }

    /**
     * 导出车辆故障管理列表
     */
    @RequiresPermissions("breakdown:breakdown:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCarFault sysCarFault)
    {
        List<SysCarFault> list = sysCarFaultService.selectSysCarFaultList(sysCarFault);
        ExcelUtil<SysCarFault> util = new ExcelUtil<SysCarFault>(SysCarFault.class);
        util.exportExcel(response, list, "车辆故障管理数据");
    }

    /**
     * 获取车辆故障管理详细信息
     */
    @RequiresPermissions("breakdown:breakdown:query")
    @GetMapping(value = "/{id}")
    public Result<List<SysCarFault>> getInfo(@PathVariable("id") Long id)
    {
        return success(sysCarFaultService.selectSysCarFaultById(id));
    }

    /**
     * 新增车辆故障管理
     */
    @RequiresPermissions("breakdown:breakdown:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody SysCarFault sysCarFault)
    {
        if (sysCarFaultService.checkIdUnique(sysCarFault)) {
            return error("新增 车辆故障管理 ,失败，车辆故障码已存在");
        }
        sysCarFault.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysCarFaultService.save(sysCarFault));
    }


    /**
     * 修改车辆故障管理
     */
    @RequiresPermissions("breakdown:breakdown:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody SysCarFault sysCarFault)
    {
        if (!sysCarFaultService.checkIdUnique(sysCarFault)) {
            return error("修改 车辆故障管理 '" + sysCarFault + "'失败，车辆故障管理不存在");
        }
        sysCarFault.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysCarFaultService.updateById(sysCarFault));
    }

    /**
     * 删除车辆故障管理
     */
    @RequiresPermissions("breakdown:breakdown:remove")
    @DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        sysCarFaultService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }

    /**
     * 启用告警
     * @param id
     */
    @PutMapping("/enableWarningsById/{id}")
    public void enableWarningsById(@PathVariable("id")Long id){
        UpdateWrapper<SysCarFault> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("warn_status",0);
       sysCarFaultService.update(wrapper);
    }
    /**
     * 禁用告警
     * @param id
     */
    @PutMapping("/disableWarningsById/{id}")
    public void disableWarningsById(@PathVariable("id")Long id){
        UpdateWrapper<SysCarFault> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("warn_status",1);
        sysCarFaultService.update(wrapper);
    }

}
